package com.randi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.randi.common.core.domain.Result;
import com.randi.common.enums.ResultCode;
import com.randi.common.utils.page.PageHelperUtils;
import com.randi.system.domain.entity.sysLog.SysLogOperate;
import com.randi.system.domain.params.sysLog.SysLogOperateParam;
import com.randi.system.service.sysLog.ISysLogOperateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ziye
 */
@RestController
@RequestMapping("system/log/operate")
@Api(tags = "操作日志")
public class SysLogOperateController {

    @Resource
    private ISysLogOperateService iSysLogOperateService;

    /**
     * 获取列表
     *
     * @param sysLogOperateParam 检索参数
     * @return Result
     */
    @PostMapping("/list")
    @ApiOperation(value = "获取列表", notes = "")
    @PreAuthorize("@auth.hasPermission('system:log:operate:list')")
    public Result selectList(@RequestBody SysLogOperateParam sysLogOperateParam) {
        PageHelperUtils.initPage(sysLogOperateParam);
        PageInfo<SysLogOperate> sysLogOperates = iSysLogOperateService.selectPageList(sysLogOperateParam);
        return Result.success().putPage(sysLogOperates);
    }

    /**
     * 查看详情
     *
     * @param id id
     * @return Result
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "查看详情", notes = "")
    @PreAuthorize("@auth.hasPermission('system:log:operate:list')")
    public Result selectById(@PathVariable Long id) {
        SysLogOperate sysLogOperate = iSysLogOperateService.selectSysLogOperateById(id);
        return Result.success().put(sysLogOperate);
    }

    /**
     * 插入数据
     *
     * @param sysLogOperate 业务信息
     * @return 结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "插入数据", notes = "")
    @PreAuthorize("@auth.hasPermission('system:log:operate:add')")
    public Result insert(@RequestBody SysLogOperate sysLogOperate) {
        int status = iSysLogOperateService.insertSysLogOperate(sysLogOperate);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_INSERT_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_INPUT_SUCCESS);
    }

    /**
     * 删除数据
     *
     * @param ids ids
     * @return 结果
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除数据", notes = "")
    @PreAuthorize("@auth.hasPermission('system:log:operate:delete')")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        int status = iSysLogOperateService.deleteBatchSysLogOperate(ids);

        if (status == 0) {
            return Result.error(ResultCode.BUSINESS_DELETE_ERROR);
        }
        return Result.success(ResultCode.BUSINESS_DELETE_SUCCESS);
    }

}
